// pages/searchDynamic/searchDynamic.js
const util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
		classifyList:[
			{
				name:"综合政策",
				highlight:"active",
				type:"11"
			},
			{
				name:"政策解读",
				highlight:"active",
				type:"13"
			},
			{
				name:"动态",
				highlight:"active",
				type:"14"
			},
		],
		sortList:[
			{name:"按发布时间排序",addClass:"active",orderBy:"publish_at",},
			{name:"按浏览量排序",addClass:"",orderBy:"page_view",}
		],
		/* 排序 */
		currIndex:-1,
		/* 排序下面的背景色是否显示 */
		showModal:false,
		/* 搜索内容 */
		inputValue:"",
		dynamicList:[],
		/* 机器人移动的位置 */
		leftLooks: 280,
		topLooks: 350,
		/* 初始化页码 */
		page:1,
		/* 选择地区 */
		area:"",
		/* 类型 */
		type:"11,13,14",
		/* 选择的所有类型 */
		types:[11,13,14],
		/* 没有数据显示 */
		noneData:false,
		/* 排列顺序 */
		orderBy:"publish_at",
		/* 列表margin */
		MarginTop:0,
		newHeight:0,
		Height:0,
		/* 手机宽高 */
		phoneHeight:0,
		phoneWidth:0,
		/* 是否下拉刷新 */
		refresh:false,
		/* 来源类型 */
		/* 创建多个自定义组件 */
		pages:[1],
		/* 当前页面的page值 */
		pageLength:1,
		/* 最多页码 */
		maxPage:2,
		/* 状态 */
		status:"2,3,4",
  },
	/* 跳转到问题页面 */
	toQuestion:function(){
		wx.navigateTo({
			url:"../questions/questions"
		})
	},
	/* 滑动机器人 */
	imagetouchmove:function(e){
		var phoneWidth=this.data.phoneWidth;
		var phoneHeight=this.data.phoneHeight;
		var robotWidth=wx.getStorageSync("robotWidth");
		var robotHeight=wx.getStorageSync("robotHeight");
		e.touches[0].clientX=e.touches[0].clientX<0?0:e.touches[0].clientX;
		e.touches[0].clientY=e.touches[0].clientY<0?0:e.touches[0].clientY;
		e.touches[0].clientX=e.touches[0].clientX>phoneWidth-robotWidth?phoneWidth-robotWidth:e.touches[0].clientX;
		e.touches[0].clientY=e.touches[0].clientY>phoneHeight-robotHeight?phoneHeight-robotHeight:e.touches[0].clientY;
		
		this.setData({
			leftLooks: e.touches[0].clientX,
			topLooks: e.touches[0].clientY
		})
	},
	/* 跳转到搜索页面 */
	toSearch:function(){
		wx.navigateTo({
			url: "../searchPage/searchPage"
		})
	},
	/* 切换高亮 */
	changeItem:function(e){
		var highlight=e.currentTarget.dataset.highlight?"":"active";
		this.data.classifyList[e.currentTarget.dataset.index].highlight=highlight;
		if(this.data.types[0]==-2){
			this.data.types=[];
		}
		if(highlight){
			this.data.types.push(e.currentTarget.dataset.type);
		}else{
			for(var i=0;i<this.data.types.length;i++){
				if(this.data.types[i]==e.currentTarget.dataset.type){
					this.data.types.splice(i,1);
					break;
				}
			}
			if(!this.data.types.length){
				this.data.types=[-2];
			}
		}
		this.setData({
			classifyList:this.data.classifyList,
			type:this.data.types.join(","),
			pages:[],
			pageLength:0,
		})
		wx.setStorageSync('maxPage',1);
		this.queryArticle();
	},
	/* 排序切换 */
	changeSort:function(e){
		for(var i=0;i<this.data.sortList.length;i++){
			this.data.sortList[i].addClass="";
		}
		this.data.sortList[e.currentTarget.dataset.index].addClass="active";
		this.setData({
			currIndex:e.currentTarget.dataset.index,
			sortList:this.data.sortList,
			showModal:!this.data.showModal,
			orderBy:e.currentTarget.dataset.order,
			pages:[],
			pageLength:0,
		})
		wx.setStorageSync('maxPage',1);
		this.queryArticle();
	},
	/* 显示/隐藏排序 */
	showSort:function(){
		this.setData({
			showModal:!this.data.showModal
		})
	},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
		this.setData({
			inputValue:options.content,
			searchContent:options.content,
		});
		wx.showShareMenu({  
			withShareTicket:true,
			menus: ['shareAppMessage', 'shareTimeline']
		})
  },
	/* 隐藏排序modal */
	hideModal:function(){
		this.setData({
			showModal:!this.data.showModal
		})
	},
  /* 查询最新动态 */
	queryArticle:function(){
		var _this=this;
		if(wx.getStorageSync('maxPage')>this.data.pageLength){
			this.data.pageLength+=1;
			this.data.pages.push(this.data.pageLength);
			this.setData({
				pages:this.data.pages,
				noneData:false,
				orderBy:this.data.orderBy,
				type:this.data.type,
				searchContent:this.data.inputValue
			});
			/* 保存最新的最大页码数 */
			var timer=setInterval(function(){
				if(wx.getStorageSync('newPage')){
					_this.data.maxPage=wx.getStorageSync('maxPage');
					wx.setStorageSync('newPage',false);
					clearInterval(timer);
				}
			},500);
		}else{
			this.setData({
				noneData:true
			})
		}
		
	 },
	/**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
		var _this=this;
		var phoneHeight=0;
		/* 获取手机可视区域宽高 */
		wx.getSystemInfo({
			success: function(res) {
				phoneHeight=res.windowHeight;
				_this.data.phoneWidth=res.windowWidth;
				_this.data.phoneHeight=res.windowHeight;
			},
		});
		
		wx.createSelectorQuery().selectAll('.search-fixed').boundingClientRect(function (rect) {
			_this.setData({
				MarginTop:rect[0].height,
				Height:phoneHeight-rect[0].height,
				newHeight:phoneHeight-rect[0].height,
			})
		}).exec();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
		if(!this.data.noneData){
			wx.setStorageSync('newPage',false);
			if(this.data.maxPage>-1){
				wx.setStorageSync('maxPage',this.data.maxPage);
			}
			this.queryArticle()
		}
  },
  /**
  * 页面相关事件处理函数--监听用户下拉动作
  */
  onPullDownRefresh: function () {
		this.setData({
			refresh:true,
			pages:[],
			pageLength:0,
		})
		wx.setStorageSync('maxPage',1);
  	this.queryArticle();
  	wx.showNavigationBarLoading() //在标题栏中显示加载
  },
	/**
		 * 用户点击右上角分享朋友
		 */
		onShareAppMessage: function (e) {
			
		},
		/**
		* 用户点击右上角分享朋友圈
		*/
		onShareTimeline: function () {
	
		 },
})